package utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import script.TestLogin;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class TestDataUtil {
    private static final Logger logger = LoggerFactory.getLogger(TestLogin.class);

    public static Object[][] buildData(String fileName,String stringKeys) throws IOException {
        logger.info("filName={} stringKeys={}",fileName,stringKeys);
        String s = FileUtils.readFileToString(new File("./data/"+fileName), "UTF-8");
        JSONArray jsonArray = JSONObject.parseArray(s);
        Object[][] testData = new Object[jsonArray.size()][];
        //拆分keys
        String[] split = stringKeys.split(",");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            Object[] caseData = new Object[split.length];
            for (int j=0 ;j<split.length;j++){
                String key = split[j];
                Object value = jsonObject.get(key);
                caseData[j] = value;
            }
            testData[i] = caseData;
        }
        logger.info("testData={}", Arrays.deepToString(testData));
        return testData;

    }
}
